//
//  LearningRecordViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LearningRecordViewController.h"
#import "LearningRecordTableViewCell.h"
#import "MineAPIManager.h"
@interface LearningRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)NSDictionary *parame;
@property(nonatomic ,strong)NSDictionary *data;
@end

@implementation LearningRecordViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lWhiteColor] backgroundColor:lClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"学习记录";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = lWhiteColor;
    [MineAPIManager GetStudyLogListWithParam:self.parame result:^(NSDictionary * _Nonnull dic) {
        if ([dic[@"code"] intValue] == 1) {
            self.data = dic[@"data"];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LearningRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LearningRecordTableViewCell"];
    
    if (!cell)
    {
        cell = [[LearningRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"LearningRecordTableViewCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    NSString *key = self.data.allKeys[indexPath.section];
    NSArray *array = self.data[key];
    cell.data = array[indexPath.row];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.allKeys.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.data.allKeys[section];
    NSArray *array = self.data[key];
    return array.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 78;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth,32)];
    header.backgroundColor = lWhiteColor;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lScreenWidth - 20, 22)];
    titleLabel.font = MFontWithSize(16);
    NSString *key = self.data.allKeys[section];
    titleLabel.text = key;
    [titleLabel setTextAlignment:NSTextAlignmentLeft];
    titleLabel.textColor = rgba(51, 51, 51, 1);
    [header addSubview:titleLabel];
    return header;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, lScreenHeight - lBottomDangerousAreaHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lWhiteColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
       
    }
    
    return _tableView;
}
- (void)setParame:(NSDictionary *)parame
{
    self.parame = (NSDictionary *)parame;
}
@end
